window._a = 1e3*60*5;
var trimString = function(s) {
	return !s ? "" : ("" + s).trim();
}
String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
$(document).ready(function() {
  var rt = window.external.mxGetRuntime();
	//$("#logFrame").get(0).contentWindow.location.reload();
	var account = "未登录";
	window.setInterval(function(){
		var a = account;
		if(rt.storage){
		  a = trimString(rt.storage.getConfig("account"));
		  var logs = trimString(rt.storage.getConfig("logs"));
		  localStorage["logs"] = logs;
		}
		if(a!=account){
		  account = a;
		  $("#account").html("登录账号："+account);
		}
		loadLog();
	},1000);
  //src="http://116.228.9.78:8031/lts/log.jsp?getmsg=1&account=cc1688&date=2014-11-15" 
  //var logSrc = "http://116.228.9.78:8031/lts/log.jsp?getmsg=1&account="+account+"&date=2014-11-15";
/*rt.listen("logmsg", function(msg){
	$("#logmsg").html(msg);
});*/

	var ccs = "1,2,3,4,5,6,7,8,9,10,11".split(",");
	$.each(ccs,function(i,v) {
		var htm = " <span><input name='cq_cc' value="+v+" type='checkbox'/>"+v+"</span>";
		htm += v==6?"</br>":"";
		$("div[id=cq_cc]").append(htm);
		var htm = " <span><input name='dy_cc' value="+v+" type='checkbox'/>"+v+"</span>";
		htm += v==6?"</br>":"";
		$("div[id=dy_cc]").append(htm);
		var htm = " <span><input name='cwzq_cc' value="+v+" type='checkbox'/>"+v+"</span>";
		htm += v==6?"</br>":"";
		$("div[id=cwzq_cc]").append(htm);
	});
	$("span").has("input[name$=_cc]").click(function(){
		if(event.target.tagName=="SPAN"){
			 $(this).find("input")[0].click();
			 return;
		}
		var name = $(this).find("input").attr("name");
		console.log(name);
		var ckd = $(this).find("input")[0].checked;
		var v = $(this).find("input").val();
		var cc = JSON.parse(localStorage[name]?localStorage[name]:"{}");
		cc[v] = ckd?1:0;
		localStorage[name] = JSON.stringify(cc);
		msg();
	});
	$("span").has("input[id^=kg]").click(function(){
		if(event.target.tagName=="SPAN"){
			 $(this).find("input")[0].click();
			 return;
		}
		var ckd = $(this).find("input")[0].checked;
		var id = $(this).find("input").attr("id");
		var kg = JSON.parse(localStorage["kg"]?localStorage["kg"]:"{}");
		kg[id] = ckd?1:0;
		localStorage["kg"] = JSON.stringify(kg);
		msg();
	});
	$("input[type!=checkbox]").keyup(function(){
		localStorage[this.id] = this.value;
		msg();
	});
	var tm;
	function msg(){
		clearTimeout(tm);
		$("#msg").html("选项已保存.");
		tm = setTimeout(function() {
			$("#msg").html("");
		}, 750);
		var rt = window.external.mxGetRuntime();
		rt.storage.setConfig("localStorage", JSON.stringify(localStorage));
	}
	$("#button").click(function() {
		save_options();
	});
	// Saves options to localStorage.
	function save_options() {
		$("input[type!=checkbox]").each(function(){
			localStorage[this.id] = this.value;
		});
		var dy_cc = JSON.parse(localStorage["dy_cc"]?localStorage["dy_cc"]:"{}");
		$("input[name=dy_cc]").each(function(){
			dy_cc[this.value] = this.checked?1:0;
		});
		localStorage["dy_cc"] = JSON.stringify(dy_cc);
		
		var cq_cc = JSON.parse(localStorage["cq_cc"]?localStorage["cq_cc"]:"{}");
		$("input[name=cq_cc]").each(function(){
			cq_cc[this.value] = this.checked?1:0;
		});
		localStorage["cq_cc"] = JSON.stringify(cq_cc);
		
		var cwzq_cc = JSON.parse(localStorage["cwzq_cc"]?localStorage["cwzq_cc"]:"{}");
		$("input[name=cwzq_cc]").each(function(){
			cwzq_cc[this.value] = this.checked?1:0;
		});
		localStorage["cwzq_cc"] = JSON.stringify(cwzq_cc);
		
		var kg = JSON.parse(localStorage["kg"]?localStorage["kg"]:"{}");
		$("input[id^=kg]").each(function(){
			kg[this.id] = this.checked?1:0;
		});
		localStorage["kg"] = JSON.stringify(kg);
		msg();
	};
	function load_options() {
		$("input[type!=checkbox]").each(function(){
			$("#"+this.id).val(trimString(localStorage[this.id]));
		});
		$("input[name$=_cc]").each(function(){
			var name = $(this).attr("name");
			var cc = JSON.parse(localStorage[name]?localStorage[name]:"{}");
			$(this).attr("checked",trimString(cc[$(this).val()])=="1"?true:false);
		});
		$("input[id^=kg]").each(function(){
			var kg = JSON.parse(localStorage["kg"]?localStorage["kg"]:"{}");
			$(this).attr("checked",trimString(kg[$(this).attr("id")])=="1"?true:false);
		});
	}
	load_options();
});
//升序
var sortUp = function(x,y){
	return x.logTime - y.logTime;
};
var log = function(obj) {
	if (window.console) {
		console.log(obj);
	}
};
function getTodayEN() {
	var n = new Date()
	,y = n.getFullYear()
	,m = n.getMonth() + 1
	,d = n.getDate()
	,c;
	if (d < 10) {
		d = "0"+d;
	}
	c = d + "-";
	if (m < 10) {
		c += "0";
	}
	c += m + "-";
	c += y + "";
	return c;
}
function getToday() {
    var n = new Date()
    ,y = n.getFullYear()
    ,m = n.getMonth() + 1
    ,d = n.getDate()
    ,c = y + "-";
    if (m < 10) {
        c += "0";
    }
    c += m + "-";
    if (d < 10) {
        c += "0";
    }
    c += d + "";
    return c;
}
var loadLog = function(){
	//var logSrc = "http://116.228.9.78:8031/lts/log.jsp?getmsg=1&account="+account+"&date="+getToday();
	//$("#logFrame").attr("src",logSrc);
	//var data = {"logDate":"20200408","logTime":"202004081911","logAccount":"cc333111","logMsg":"getScript done gitee indexDBTool.js! 使用备用服务器oschina-码云获取代码成功!"}
	//sessionStorage["logs"] = JSON.stringify(data);
		  
	$("#logmsg div").remove();
	var logsArr = JSON.parse(localStorage["logs"] ? localStorage["logs"] : "[]");
	var today = getToday();
	logsArr.sort(sortUp);
	var rt = window.external.mxGetRuntime();
	$.each(logsArr,function(a,b){
		if(b.logDate == today){
			var account = trimString(rt.storage.getConfig("account"));
			if(b.logAccount == account){
				var logmsg = b.logMsg;
				var loghtm = " <div style='color:green;'>"+logmsg+"</div>";
				$("#logmsg").prepend(loghtm);
			}
		}else{
			logsArr.splice(a,1);
			rt.storage.setConfig("logs", JSON.stringify(logsArr));
			
		}
	});
}
function reloadLog(){
	//$("#logFrame").get(0).contentWindow.location.reload();
	loadLog();
}